import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

public class P0P3Client {
    private static BufferedReader in; //从套接字中读取文本
    private static PrintStream out; // 将文本写入套接字
    public static void main(java.lang.String[] args) {
        //用户名
        String username = "";
        //密码
        String password = "";
        //Pop服务器名
        String popServer = "";
        Socket s;
        String line;
        Scanner scanner=new Scanner(System.in);
        System.out.println("please enter pop server(like pop.sina.com)");
        popServer=scanner.nextLine();
        System.out.println("please enter pop username(like johnson_co@sina.com)");
        username=scanner.nextLine();
        System.out.println("please enter password(like zsywdbb1314@)");
        password=scanner.nextLine();
        try {
            System.out.println("Connecting to " + popServer + " ...");
            //Pop3服务默认使用110端口
            //建立TCP连接
            s = new Socket(popServer,110);
            System.out.println("Connected\nAuthentication in progress...");
            //连接建立成功，获得关联的输入流和输出流
            in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            out = new PrintStream(s.getOutputStream());
            //这一行响应是服务器的欢迎信息，可抛弃
            line = readFromInputStream();
            //发送用户名
            user(username,s);
            //发送密码
            pass(password,s);
            System.out.println("please ender commend(stat,list,retr,dele,quit)");
            while (scanner.hasNext()){
                String commend= scanner.nextLine();
                if(commend.equalsIgnoreCase("stat")){
                    STAT(s);
                }else if(commend.equalsIgnoreCase("list")){
                    LIST(s);
                }else if(commend.contains("retr")){
                    retr(commend,s);
                }else if(commend.contains("dele")){
                    dele(commend,s);
                }else if(commend.equalsIgnoreCase("quit")){
                    quit(s);
                    //关闭套接字
                    s.close();
                    break;
                }else {
                    System.out.println("please ender commend(stat,list,retr dele ,quit)");
                }
                System.out.println("please ender commend(stat,list,retr,dele,quit)");
            }
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
    }

    private static void quit( Socket s) throws IOException {
        printToOutputStream("QUIT");
        readFromInputStream();
    }

    private static void dele(String commend, Socket s) throws IOException {
        printToOutputStream(commend);
        readFromInputStream();
    }

    private static void retr(String commend, Socket s) throws IOException {
        printToOutputStream(commend);
        //从服务器读取消息
        String line = readFromInputStream();
        if (!line.startsWith("+OK")) {
            System.out.println("Error: " + line);
            s.close();
            System.exit(1);
        }
        while (line.compareTo(".") != 0) {
            if (line.compareTo("..") == 0)
                System.out.println(".");
            else
                System.out.println(line);
            //读取下一行
            line = in.readLine();
        }
    }

    private static void LIST(Socket s) throws IOException {
        //命令list
        printToOutputStream("LIST");
        //读取响应
        String line = readFromInputStream();
        if (!line.startsWith("+OK")) {
            //如果命令执行失败，则退出
            System.out.println("Error:" + line);
            s.close();
            System.exit(1);
        }
        while (line.compareTo(".") != 0) {
            if (line.compareTo("..") == 0)
                System.out.println(".");
            else
                System.out.println(line);
            //读取下一行
            line = in.readLine();
        }
    }

    private static void STAT(Socket s) throws IOException {
        //命令STAT
        printToOutputStream("STAT");
        //读取响应
        String line = readFromInputStream();
        if (!line.startsWith("+OK")) {
            //如果命令执行失败，则退出
            System.out.println("Error:" + line);
            s.close();
            System.exit(1);
        }
        //从统计信息中抽取消息个数
        int i = line.lastIndexOf(' ');
    }

    private static void pass(String password, Socket s) throws IOException {
        //用户名存在，则发送密码
        out.println("PASS " + password);
        System.out.println("R: PASS ********");
        //读服务器响应信息
        String line = readFromInputStream();
        //如果密码核对成功，响应信息中开头+OK字样
        while (!line.startsWith("+OK")) {
            //密码不正确，无法继续
            System.out.println("Wrong password， disconnecting");
            s.close();
            System.exit(1);
        }
    }

    private static void user(String username,Socket s) throws IOException {
        printToOutputStream("USER " + username);
        //如果用户不存在，则响应信息开头有+OK字样。否则退出
        if (!readFromInputStream().startsWith("+OK")) {
            System.out.println("Wrong user name， disconnecting");
            s.close();
            System.exit(1);
        }
    }

    // 方法printToOutputStream()向服务器发送命令
    private static void printToOutputStream(String s) throws IOException {
        System.out.println("S : " + s);
        out.println(s);
        return;
    }
    // 方法readFromInputStream()接收从服务器发回的响应信息
    private static String readFromInputStream() throws IOException {
        String s = in.readLine();
        if (s != null)
            System.out.println("R :" + s);
        return s;
    }
}
